// import axios from "axios";
// import api from "@/api";
import { toLogout, systemPrivilegeInfo } from "../../api/api";
const user = {
  state: {
    username: "",
    userid: "",
    system: process.env.VUE_APP_SYS_NAME,
    systemRight: "",
    roleCode: "", // 角色code
    roleName: "", // 角色名称
    roles: [], // 角色列表
    tabs: []
  },
  getters: {
    GET_ROLECODE(state) {
      return state.roleCode;
    }
  },
  mutations: {
    SET_SYSTEM: (state, system) => {
      state.system = system;
    },
    SET_SYSTEMRIGHT: (state, val) => {
      state.systemRight = val;
    },
    SET_USERNAME: (state, username) => {
      state.username = username;
    },
    SET_USERID: (state, userid) => {
      state.userid = userid;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_ROLECODE: (state, roleCode) => {
      state.roleCode = roleCode;
    },
    SET_ROLENAME: (state, roleName) => {
      state.roleName = roleName;
    },
    SET_TABS: (state, tabs) => {
      state.tabs = tabs;
    }
  },
  actions: {
    setSystemAction({ commit }, indexData) {
      // console.log("indexData", indexData);
      if (indexData.username) {
        commit("SET_USERNAME", indexData.username);
      }
      if (indexData.userid) {
        commit("SET_USERID", indexData.userid);
      }
      if (indexData.systemname) {
        commit("SET_SYSTEM", indexData.systemname);
      }
      if (indexData.identifyList) {
        commit("SET_ROLES", indexData.identifyList);
      }
      if (indexData.roleCode) {
        commit("SET_ROLECODE", indexData.roleCode);
      }
      if (indexData.roleName) {
        commit("SET_ROLENAME", indexData.roleName);
      }
    },
    // 获取用户权限
    queryUserRole({ commit }, indexData) {
      commit("SET_TABS", indexData.tabs);
      commit("SET_ROLES", indexData.roles);
      commit("SET_SYSTEM", indexData.system);
      commit("SET_USERNAME", indexData.username);
    },
    // 登出
    async systemPrivilegeInfoFn({ commit }) {
      // commit("SET_ROLES", []);
      // commit("SET_TABS", []);
      // commit("SET_USERNAME", "");
      // commit("SET_USERID", "");
      // commit("SET_SYSTEM", "");
      // if (this.system) {
      //   console.log("sys", this.system);
      // }
      try {
        const res = await systemPrivilegeInfo();
        commit("SET_SYSTEM", res.data.systemName);
        commit("SET_SYSTEMRIGHT", res.data.footerSystemName);
      } catch (error) {
        // console.error(error);
      }
    },
    // 登出
    async logOut({ commit }) {
      commit("SET_SYSTEM", "");
      commit("SET_SYSTEMRIGHT", "");
      commit("SET_USERNAME", "");
      commit("SET_USERID", "");
      commit("SET_ROLES", []);
      commit("SET_ROLECODE", "");
      commit("SET_ROLENAME", "");
      commit("SET_TABS", []);
      try {
        await toLogout();
        await this.logOutAfterFn();
        // console.log("res", res);
      } catch (error) {
        // console.error(error);
      }
    },
    async logOutAfterFn() {
      localStorage.removeItem("username");
      localStorage.removeItem("mediaFullPath");
      sessionStorage.removeItem("userId");
      // 以下存储可以到代码中优化，暂时这里统一处理
      sessionStorage.removeItem("currentPageTask");
      sessionStorage.removeItem("witnessSubTaskId");
      sessionStorage.removeItem("searchFormTask");
      sessionStorage.removeItem("pageSizeTask1");
      sessionStorage.removeItem("searchForm");
      sessionStorage.removeItem("searchFormTask1");
      sessionStorage.removeItem("pageSizeTask");
      sessionStorage.removeItem("pageSize");
      sessionStorage.removeItem("currentPageTask1");
      sessionStorage.removeItem("token");
      sessionStorage.removeItem("currentPage");
      // await this.setSystemAction({
      //   username: "",
      //   userid: "",
      //   systemname: "",
      //   identifyList: "",
      //   roleCode: "",
      //   roleName: ""
      // });
      document.cookie = "";
      // delCookie("G3_SESSION_V");
      // await this.setHeadNavAction([]);
      // await this.setButtonListAction([]);
    }
  }
};
export default user;
